package com.zhongwang.mms.module.material.service;

import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.module.material.dao.MReportFormsDao;
import com.zhongwang.mms.module.material.model.*;
import com.zhongwang.mms.util.FileUtils;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @description:原料统计报表
 * @author: th
 * @time: 2020/7/9 9:20
 */
@Service
public class MReportFormsService {
    @Autowired
    private MReportFormsDao reportFormsDao;
    @Value("${user.upload.path}")
    private String uploadPath;

    /**
     * 机械总出库导出
     * @param response
     * @param param
     */
    public void matMechanicsOutWarExcel(HttpServletResponse response, MechanicsOutWar param) {

        List<MechanicsOutWar> model = reportFormsDao.findMatMechanicsOutWarList(param);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String path = uploadPath + user.getAccount() + "/";
        FileUtils.export(path, "机械月出库总表.xls", MechanicsOutWar.class, "sheet1", model, response);
    }

    /**
     * 外厂总出库导出
     * @param response
     * @param param
     */
    public void matOutFactoryOutWarExcel(HttpServletResponse response, MechanicsOutWar param) {

        List<MechanicsOutWar> model = reportFormsDao.findmMatOutFactoryOutWarList(param);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String path = uploadPath + user.getAccount() + "/";
        FileUtils.export(path, "外厂月出库总表.xls", MechanicsOutWar.class, "sheet1", model, response);
    }

    /**
     * 消耗库存量列表导出
     * @param response
     * @param compositeOutcomeModel
     */
    public void exportExcel(HttpServletResponse response, CompositeOutcomeModel compositeOutcomeModel) {
        compositeOutcomeModel.setOdaRedFlag(1); // 不显示红字
        List<CompositeOutcomeModel> list = reportFormsDao.findConsumedInventory(compositeOutcomeModel);
        Subject userSubject = ShiroUtils.getSubject();
        for (int i = 0; i < list.size(); i++) {
            if (!userSubject.isPermitted("matwar:amount:view") || list.get(i).getOdaPrice() == 0) {
                list.get(i).setOdaPrice(null);
                list.get(i).setOdaAmount(null);
            }
        }

        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String path = uploadPath + user.getAccount() + "/";
        FileUtils.export(path,"消耗库存量列表.xls", CompositeOutcomeModel.class, "sheet1", list, response);
    }

    /**
     * 不常用库存列表
     * @param complexStorageModel
     * @param matTypeIdList
     * @param matQualityIdList
     * @param matSpecIdList
     * @return
     */
    public List<MatInfrequentStockExport> findStorage(ComplexStorageModel complexStorageModel, List<String> matTypeIdList, List<String> matQualityIdList, List<String> matSpecIdList) {

        StringBuilder sb;

        // 类型多选条件
        if (matTypeIdList != null && matTypeIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matTypeIdList.size(); i++) {
                sb.append(matTypeIdList.get(i)).append(",");
            }
            complexStorageModel.setMatTypeIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 材质多选条件
        if (matQualityIdList != null && matQualityIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matQualityIdList.size(); i++) {
                sb.append(matQualityIdList.get(i)).append(",");
            }
            complexStorageModel.setMatQualityIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 规格多选条件
        if (matSpecIdList != null && matSpecIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matSpecIdList.size(); i++) {
                sb.append(matSpecIdList.get(i)).append(",");
            }
            complexStorageModel.setMatSpecIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }

        return reportFormsDao.findComplexStorage(complexStorageModel);
    }

    /**
     * 不常用库存导出
     * @param response
     * @param complexStorageModel
     * @param matTypeIdList
     * @param matQualityIdList
     * @param matSpecIdList
     */
    public void matInfrequentStockExportExcel(HttpServletResponse response, ComplexStorageModel complexStorageModel, List<String> matTypeIdList, List<String> matQualityIdList, List<String> matSpecIdList) {
        StringBuilder sb;

        // 类型多选条件
        if (matTypeIdList != null && matTypeIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matTypeIdList.size(); i++) {
                sb.append(matTypeIdList.get(i)).append(",");
            }
            complexStorageModel.setMatTypeIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 材质多选条件
        if (matQualityIdList != null && matQualityIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matQualityIdList.size(); i++) {
                sb.append(matQualityIdList.get(i)).append(",");
            }
            complexStorageModel.setMatQualityIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        // 规格多选条件
        if (matSpecIdList != null && matSpecIdList.size() > 0) {
            sb = new StringBuilder();
            for (int i = 0; i < matSpecIdList.size(); i++) {
                sb.append(matSpecIdList.get(i)).append(",");
            }
            complexStorageModel.setMatSpecIdQueryList("'" + StringUtils.join(sb.toString().substring(0, sb.toString().length() - 1).split(","), "','") + "'");
        }
        List<MatInfrequentStockExport> model = reportFormsDao.findComplexStorage(complexStorageModel);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String path = uploadPath + user.getAccount() + "/";
        FileUtils.export(path, "不常用库存.xls", MatInfrequentStockExport.class, "sheet1", model, response);

    }

    /**
     * 阶段报表导出
     * @param response
     * @param param
     */
    public void matStageReportExcel(HttpServletResponse response, MatStageReportModel param) {

        List<MatStageReportModel> model = reportFormsDao.findMatStageReportList(param);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String path = uploadPath + user.getAccount() + "/";
        FileUtils.export(path, "阶段报表.xls", MatStageReportModel.class, "sheet1", model, response);
    }
}
